package com.bd.scm.module.common;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bd.scm.domain.dto.aggrid.AgGridRequest;
import com.bd.scm.domain.dto.aggrid.AgGridResponse;
import com.bd.scm.module.utils.HttpServeletUtil;
import com.bd.utils.base.BaseClass;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class BaseController extends BaseClass {

    public <T> Page<T>  getPage(AgGridRequest request){
        int pageNo = 1;
        int pageSize = request.getEndRow() - request.getStartRow();

        if (request.getStartRow() > 0) {
            pageNo = request.getStartRow() / pageSize + 1;
        }

        Page<T> page = new Page<T>(pageNo, pageSize);

        return page;
    }

    public <T> int getLastRow(AgGridRequest request, IPage<T> pageList){
        int diff = request.getEndRow() - request.getStartRow();
        int dataSize = pageList.getRecords().size();
        int lastRow = dataSize < diff ? dataSize : diff + dataSize;

        return lastRow;
    }

    public <T> AgGridResponse<T> getResponse(AgGridRequest request, IPage<T> pageList, Page<T> page) {
        AgGridResponse<T> response = new AgGridResponse<>();

        response.setData(pageList.getRecords());

        int lastRow=getLastRow(request, page);
        response.setLastRow(lastRow);

        if(page.getPages()==0){
            response.setHasRecords(0);
        }else {
            if (page.getCurrent() == page.getPages()) {
                response.setHasRecords(0);
            } else {
                response.setHasRecords(1);
            }
        }

        return response;
    }

    public <T> AgGridResponse<T> getResponse2(AgGridRequest request, List<T> pageList, Page<T> page) {
        AgGridResponse<T> response = new AgGridResponse<>();

        response.setData(pageList);

        int lastRow=getLastRow(request, page);
        response.setLastRow(lastRow);

        if(page.getCurrent()==page.getPages()){
            response.setHasRecords(0);
        }else{
            response.setHasRecords(1);
        }

        return response;
    }

    /**
     * 获取当前登录 人ID
     * @param request
     * @return
     */
    public Long getLoginUserId (HttpServletRequest request) {
        JSONObject user  = (JSONObject) request.getSession().getAttribute("user");
        if(null == user) {
            return 0L;
        }

        String userSerial = user.getString("userSerial");
        if(StringUtils.isEmpty(userSerial)) {
            return 0L;
        }

        return Long.parseLong(userSerial);
    }

    public Long getLoginUserId () {
        JSONObject user = getSessionUser();

        if(null == user) {
            return 0L;
        }

        String userSerial = user.getString("userSerial");
        if(StringUtils.isEmpty(userSerial)) {
            return 0L;
        }

        return Long.parseLong(userSerial);
    }

    public JSONObject getSessionUser() {
        HttpServletRequest request = HttpServeletUtil.getRequest();
        JSONObject user  = (JSONObject) request.getSession().getAttribute("user");

//        if (ObjectUtil.isNull(user)) {
//            throw new ScmException(ExceptionEmun.LOGIN_TIMEOUTORNOTLOGIN);
//        }

        return user;
    }

    /**
     * 获取当前登录人部门权限，存在session中
     * @param request
     * @return 返回List
     */
    public List<String> getLoginUserDeptList(HttpServletRequest request) {
        return (List<String>) request.getSession().getAttribute("findDeptid");
    }

    /**
     * 获取当前登录人部门权限，存在session中
     *
     * @param request
     * @return 返回逗号隔开的字符串
     */
    public String getLoginUserDept(HttpServletRequest request) {
        List<String> deptList = getLoginUserDeptList(request);

//        if (ObjectUtil.isEmpty(deptList)) {
//            throw new ScmException(ExceptionEmun.LOGIN_TIMEOUTORNOTLOGIN);
//        }

        //部门权限为空，表示是超级管理员，可以查看所有部门
        if (CollectionUtils.isEmpty(deptList))  {
            return "";
        }
        return StringUtils.join(deptList, ",");
    }

    /**
     * 获取当前登录人部门权限，存在session中
     * @return
     */
    public String getLoginUserDept() {
        HttpServletRequest request = HttpServeletUtil.getRequest();
        return getLoginUserDept(request);
    }


}
